package traversal;

import java.util.Stack;

public class PreOrderTraversal {
    private static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val)
        {
            this.val = val;
        }
    }

    public void preorderTraversal(TreeNode root)
    {
        if(root == null) return ;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty())
        {
            TreeNode node = stack.pop();
            System.out.println(node.val);
            if(node.right != null)
            {
                stack.push(node.right);
            }
            if(node.left != null)
            {
                stack.push(node.left);
            }
        }
    }
}
